package com.stylefeng.guns.modular.orderpush.controller;

import com.stylefeng.guns.config.handler.WsHandler;
import com.stylefeng.guns.core.base.controller.BaseController;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.orderpush.model.Order;
import com.stylefeng.guns.modular.orderpush.service.IOrderService;
import org.springframework.web.socket.TextMessage;

import java.io.IOException;
import java.util.List;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-05-07 21:19:51
 */
@Controller
@RequestMapping("/order")
public class OrderController extends BaseController {

    private String PREFIX = "/orderpush/order/";

    @Autowired
    private IOrderService orderService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "order.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/order_add")
    public String orderAdd() {
        return PREFIX + "order_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/order_update/{orderId}")
    public String orderUpdate(@PathVariable Integer orderId, Model model) {
        Order order = orderService.selectById(orderId);
        model.addAttribute("item",order);
        LogObjectHolder.me().set(order);
        return PREFIX + "order_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return orderService.selectList(null);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Order order) {
        orderService.insert(order);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer orderId) {
        orderService.deleteById(orderId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Order order) {
        orderService.updateById(order);
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{orderId}")
    @ResponseBody
    public Object detail(@PathVariable("orderId") Integer orderId) {
        return orderService.selectById(orderId);
    }

    /**
     * 推送页面
     */
    @RequestMapping(value = "/push")
        public String orderPush(){
            List orders = orderService.selectList(null);
            super.setAttr("orders",orders);

        //WebSocketServer.sendInfo("");






        //orderService.megProducer();
        //orderService.megConsumer();
        return PREFIX+"order_push.html";

    }

    @MessageMapping("/hello")
    @SendTo("/topic/hello")
    public String greeting(String message) throws Exception {

        Thread.sleep(1000); // simulated delay
        return "Hello, client! I get your message";
    }

    @RequestMapping(value = "/sendMsg/{msg}")
    @ResponseBody
    public String sendMsg(@PathVariable String msg){

        TextMessage  textMessage = new TextMessage(msg);
        WsHandler.sendMessageToUsers(textMessage);
        return "success : "+msg;
    }

}
